package personnel.department.pages.addpage;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;

import personnel.department.dao.entity.Qualification;
import personnel.department.pages.PDTable;
import personnel.department.pages.widgets.AbstractWidget;
import personnel.department.utils.DateUtils;

public class QualificationEditor extends AbstractTableEditor<Qualification> {

	public QualificationEditor(CTabFolder tabFolder) {
		super(tabFolder, "Квалификация");
	}

	public QualificationEditor(CTabFolder tabFolder, Set<Qualification> qualifications) {
		super(tabFolder, "Квалификация", qualifications);
	}

	@Override
	public PDTable<Qualification> createTable(Composite parent) {
		return new PDTable<Qualification>(parent) {

			@Override
			public List<Qualification> setModelProvider() {
				return getModel();
			}

			@Override
			protected void createColumns(Composite parent, TableViewer viewer) {
				String[] titles = { "Организация", "Имя цикла", "Где пройдено", "Часы", "Дата тестирования", 
						"номер сертификата", "Начало обучения", "Конец обучения", "Начальная квалификация"};
				int[] bounds = { 100, 100, 100, 100, 130, 130, 130, 130, 160};

				TableViewerColumn col = createTableViewerColumn(getViewer(), titles[0], bounds[0], 0);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getOrganization());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[1], bounds[1], 1);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getNameCycle());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[2], bounds[2], 2);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getWhereCompleted());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[3], bounds[3], 3);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getHours());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[4], bounds[4], 4);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return format.format(e.getDateTesting());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[5], bounds[5], 5);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getNumberCertificate());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[6], bounds[6], 6);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return format.format(e.getStartEducation());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[7], bounds[7], 7);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return format.format(e.getFinishEducation());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[8], bounds[8], 8);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Qualification e = (Qualification) element;
						return String.valueOf(e.getFirstQualification());
					}
				});

			}
		};
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() {
		AbstractWidget<?> firstQualification = getWidgetByFieldName("firstQualification");
		boolean present = getModel().stream().
				filter(qual -> qual.getFirstQualification())
				.findAny()
				.isPresent();
		if (present) {
			firstQualification.setEnabled(false);
		}

		AbstractWidget<Date> dateTesting = (AbstractWidget<Date>) getWidgetByFieldName("dateTesting");

		if (!getModel().isEmpty()) {
			Date maxConfirmation = getModel().stream()
					.max((arg0, arg1) -> arg0.getDateTesting().compareTo(arg1.getDateTesting()))
					.map(qual -> qual.getDateTesting())
					.get();

			dateTesting.setValue(DateUtils.add(maxConfirmation, Calendar.YEAR, 5));
		}
	}

}
